package virtorina.server.rpc;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;

import virtorina.client.admin.dto.LabelDTO;
import virtorina.client.room.dto.RoomInfo;
import virtorina.client.room.rpc.GWTRoomsService;
import virtorina.server.Base;
import virtorina.server.domain.Label;
import virtorina.server.domain.Question;
import virtorina.server.domain.Room;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class RoomsImpl extends RemoteServiceServlet implements GWTRoomsService {

	private static final long serialVersionUID = 1L;

	@Override
	public LinkedList<RoomInfo> RoomsList() {
		LinkedList<RoomInfo> list = new LinkedList<RoomInfo>();
		RoomInfo ri;
		LinkedList<String> labels;
		for (Room r : Base.getRooms()) {
			ri = new RoomInfo();
			ri.setName(r.getName());
			labels = new LinkedList<String>(Label.getLabelsNames(r.getLabels()));
			if (r.getTypes().contains(3)) {
				labels.addFirst("#!Map type");
			}
			if (r.getTypes().contains(2)) {
				labels.addFirst("#!Audio type");
			}
			if (r.getTypes().contains(1)) {
				labels.addFirst("#!Image type");
			}
			if (r.getTypes().contains(0)) {
				labels.addFirst("#!Text type");
			}
			ri.setLabels(labels);
			ri.setPeople(r.getOnline());
			ri.setId(r.getID());
			list.add(ri);
		}
		return list;
	}

	@Override
	public LinkedList<LabelDTO> getAllLabels() {
		return new GWTInsertQuestionsImpl().getAllLabels();
	}

	@Override
	public Integer makeRoom(LinkedHashSet<Long> labels, String name,
			LinkedList<Integer> types) {

		if (!Base.chkName(name)) {
			return -1; // такое имя уже есть
		}
		int n = Question.numOfQuestions(new LinkedList<Long> (labels), types);
		System.out.println(n);
		if (n < 5){
			return -2;
		}
		return Base.addRoom(name, labels, types);

	}

}
